package com.sxx.beautifulcity.fragment;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.activity.AgritourismActivity;
import com.sxx.beautifulcity.activity.AroundActivity;
import com.sxx.beautifulcity.activity.DeliciousFoodActivity;
import com.sxx.beautifulcity.activity.HolidayVillageActivity;
import com.sxx.beautifulcity.activity.LineActivity;
import com.sxx.beautifulcity.activity.PetActivity;
import com.sxx.beautifulcity.activity.PluckActivity;
import com.sxx.beautifulcity.activity.SpecialityActivity;
import com.sxx.beautifulcity.activity.VillageActivity;
import com.sxx.beautifulcity.adapter.HomeAdapter;
import com.sxx.beautifulcity.application.ProjectApplication;
import com.sxx.beautifulcity.listenerInterface.OnHomeItemClickListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by dllo on 15/10/29.
 */
public class HomeFragment extends Fragment implements OnHomeItemClickListener {
    private GridLayoutManager manager;
    private RecyclerView home_recyclerView;
    private ImageView water, wate, water_f, water_i, water_r, water_s, water_t, water_u, water_e, water_c, water_o, water_n, water_d;

    private String[] homes = {"农家乐", "度假村", "采摘",
            "乡村", "特产", "美食", "家有宠物", "美图", "精品路线",
            "优惠促销", "景区周边", "花鸟鱼虫"};
    private int[] colors = {ProjectApplication.getContext().getResources().getColor(R.color.c_agri),
            ProjectApplication.getContext().getResources().getColor(R.color.c_holday),
            ProjectApplication.getContext().getResources().getColor(R.color.c_pluck),
            ProjectApplication.getContext().getResources().getColor(R.color.c_village),
            ProjectApplication.getContext().getResources().getColor(R.color.c_spectial),
            ProjectApplication.getContext().getResources().getColor(R.color.c_fine_food),
            ProjectApplication.getContext().getResources().getColor(R.color.c_pet),
            ProjectApplication.getContext().getResources().getColor(R.color.c_beautiful_image),
            ProjectApplication.getContext().getResources().getColor(R.color.c_line),
            ProjectApplication.getContext().getResources().getColor(R.color.c_sale),
            ProjectApplication.getContext().getResources().getColor(R.color.c_around),
            ProjectApplication.getContext().getResources().getColor(R.color.c12)};
    private List<Map<String, Object>> list = new ArrayList<>();
    private HomeAdapter adapter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        manager = new GridLayoutManager(context, 3);
        adapter = new HomeAdapter();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        home_recyclerView = (RecyclerView) view.findViewById(R.id.home_recyclerView);
        water = (ImageView) view.findViewById(R.id.water);
        wate = (ImageView) view.findViewById(R.id.wate);
        water_f = (ImageView) view.findViewById(R.id.water_f);
        water_i = (ImageView) view.findViewById(R.id.water_i);
        water_r = (ImageView) view.findViewById(R.id.water_r);
        water_s = (ImageView) view.findViewById(R.id.water_s);
        water_t = (ImageView) view.findViewById(R.id.water_t);
        water_u = (ImageView) view.findViewById(R.id.water_u);
        water_e = (ImageView) view.findViewById(R.id.water_e);
        water_c = (ImageView) view.findViewById(R.id.water_c);
        water_o = (ImageView) view.findViewById(R.id.water_o);
        water_n = (ImageView) view.findViewById(R.id.water_n);
        water_d = (ImageView) view.findViewById(R.id.water_d);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        init();

    }

    private void init() {
        for (int i = 0; i < homes.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("homes", homes[i]);
            map.put("colors", colors[i]);
            list.add(map);
        }

        home_recyclerView.setLayoutManager(manager);
        home_recyclerView.setAdapter(adapter);
        adapter.addData(list);
        adapter.setOnHomeItemClickListener(this);
        ObjectAnimator animator = new ObjectAnimator().ofFloat(water, "translationY", 0f, 1280f);
        animator.setDuration(1000);
        animator.setRepeatCount(Integer.MAX_VALUE);
        animator.start();
        ObjectAnimator animator_wate = new ObjectAnimator().ofFloat(wate, "translationY", 0f, 1280f);
        animator_wate.setDuration(2000);
        animator_wate.setRepeatCount(Integer.MAX_VALUE);
        animator_wate.start();
        ObjectAnimator animator_water_f = new ObjectAnimator().ofFloat(water_f, "translationY", 0f, 1280f);
        animator_water_f.setDuration(3000);
        animator_water_f.setRepeatCount(Integer.MAX_VALUE);
        animator_water_f.start();
        ObjectAnimator animator_water_i = new ObjectAnimator().ofFloat(water_i, "translationY", 0f, 1280f);
        animator_water_i.setDuration(4000);
        animator_water_i.setRepeatCount(Integer.MAX_VALUE);
        animator_water_i.start();
        ObjectAnimator animator_water_r = new ObjectAnimator().ofFloat(water_r, "translationY", 0f, 1280f);
        animator_water_r.setDuration(5000);
        animator_water_r.setRepeatCount(Integer.MAX_VALUE);
        animator_water_r.start();
        ObjectAnimator animator_water_s = new ObjectAnimator().ofFloat(water_s, "translationY", 0f, 1280f);
        animator_water_s.setDuration(1000);
        animator_water_s.setRepeatCount(Integer.MAX_VALUE);
        animator_water_s.start();
        ObjectAnimator animator_water_t = new ObjectAnimator().ofFloat(water_t, "translationY", 0f, 1280f);
        animator_water_t.setDuration(2000);
        animator_water_t.setRepeatCount(Integer.MAX_VALUE);
        animator_water_t.start();
        ObjectAnimator animator_water_u = new ObjectAnimator().ofFloat(water_u, "translationY", 0f, 1280f);
        animator_water_u.setDuration(3000);
        animator_water_u.setRepeatCount(Integer.MAX_VALUE);
        animator_water_u.start();
        ObjectAnimator animator_water_e = new ObjectAnimator().ofFloat(water_e, "translationY", 0f, 1280f);
        animator_water_e.setDuration(4000);
        animator_water_e.setRepeatCount(Integer.MAX_VALUE);
        animator_water_e.start();
        ObjectAnimator animator_water_c = new ObjectAnimator().ofFloat(water_c, "translationY", 0f, 1280f);
        animator_water_c.setDuration(5000);
        animator_water_c.setRepeatCount(Integer.MAX_VALUE);
        animator_water_c.start();
        ObjectAnimator animator_water_o = new ObjectAnimator().ofFloat(water_o, "translationY", 0f, 1280f);
        animator_water_o.setDuration(1000);
        animator_water_o.setRepeatCount(Integer.MAX_VALUE);
        animator_water_o.start();
        ObjectAnimator animator_water_n = new ObjectAnimator().ofFloat(water_n, "translationY", 0f, 1280f);
        animator_water_n.setDuration(2000);
        animator_water_n.setRepeatCount(Integer.MAX_VALUE);
        animator_water_n.start();
        ObjectAnimator animator_water_d = new ObjectAnimator().ofFloat(water_d, "translationY", 0f, 1280f);
        animator_water_d.setDuration(3000);
        animator_water_d.setRepeatCount(Integer.MAX_VALUE);
        animator_water_d.start();
    }

    @Override
    public void OnHomeItemClick(String str) {
        if (str.equals(homes[0])) {
            Intent intent = new Intent(ProjectApplication.getContext(), AgritourismActivity.class);
            startActivity(intent);
            getActivity().finish();

        } else if (str.equals(homes[1])) {
            Intent holidayIntent = new Intent(ProjectApplication.getContext(), HolidayVillageActivity.class);
            startActivity(holidayIntent);
            getActivity().finish();

        } else if (str.equals(homes[2])) {
            Intent pluckIntent = new Intent(ProjectApplication.getContext(), PluckActivity.class);
            startActivity(pluckIntent);
            getActivity().finish();

        } else if (str.equals(homes[3])) {
            Intent villageIntent = new Intent();
            villageIntent.setClass(ProjectApplication.getContext(), VillageActivity.class);
            startActivity(villageIntent);
            getActivity().finish();

        } else if (str.equals(homes[4])) {
            Intent specialityIntent = new Intent();
            specialityIntent.setClass(ProjectApplication.getContext(), SpecialityActivity.class);
            startActivity(specialityIntent);
            getActivity().finish();

        } else if (str.equals(homes[5])) {
            Intent deliciousIntent = new Intent();
            deliciousIntent.setClass(ProjectApplication.getContext(), DeliciousFoodActivity.class);
            startActivity(deliciousIntent);
            getActivity().finish();

        } else if (str.equals(homes[6])) {
            Intent petIntent = new Intent();
            petIntent.setClass(ProjectApplication.getContext(), PetActivity.class);
            startActivity(petIntent);
            getActivity().finish();


        } else if (str.equals(homes[7])) {

        } else if (str.equals(homes[8])) {
            Intent lineIntent = new Intent();
            lineIntent.setClass(ProjectApplication.getContext(), LineActivity.class);
            startActivity(lineIntent);
            getActivity().finish();

        } else if (str.equals(homes[9])) {

        } else if (str.equals(homes[10])) {
            Intent aroundIntent = new Intent();
            aroundIntent.setClass(ProjectApplication.getContext(), AroundActivity.class);
            startActivity(aroundIntent);
            getActivity().finish();

        } else if (str.equals(homes[11])) {

        }

    }
}
